﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == true)
            DoPostBackAction();
        else
            CrossPagePostBackAction();
    }


    private void DoPostBackAction()
    {
        if(CheckForLogin()==false)
            return;

        string actionCode = Request.Form.Get("__EVENTTARGET");  // or string actionCode = Request.Params.Get("__EVENTTARGET");
        string arguments = Request.Form.Get("__EVENTARGUMENT");

        switch (actionCode)
        {
            case "quanlytaikhoan": QuanLyTaiKhoan();
                break;
            case "quanlysanpham_comment": QuanLySanPham_Comment();
                break;
        }
    }

    private void QuanLyTaiKhoan()
    {
    }

    private void QuanLySanPham_Comment()
    {
    }

    private void CrossPagePostBackAction()
    {
        if (this.PreviousPage != null)
        {
            if (this.PreviousPage.IsCrossPagePostBack)
            {
                string actioncode = Request.Params["act"];
                switch (actioncode)
                {
                }

                if (CheckForLogin())
                {
                    string controlID = Request.Form.Get("__EVENTTARGET");  // or string actionCode = Request.Params.Get("__EVENTTARGET");

                    string userName;
                    Control ctrl = this.PreviousPage.FindControl(controlID);
                    if (ctrl != null)
                        userName = ((LinkButton)ctrl).Text;
                    else
                        userName = Request.Cookies["daugia_user"]["username"];

                    ShowBasicProfileByName(userName);
                }
            }
        }
        else
            if (Request.Cookies["daugia_user"] != null)
            {
                string userNameLogin = Request.Cookies["daugia_user"]["username"];
                if (userNameLogin.Length > 0)
                    ShowBasicProfileByName(userNameLogin);
            }
    }

    private void ShowBasicProfileByName(string tenTaiKhoan)
    {
        string strQuery = "Select a.MaTaiKhoan, TenTaiKhoan, HoTen,Avatar from TAIKHOAN a, THONGTINTAIKHOAN b where a.TenTaiKhoan='" + tenTaiKhoan + "' and b.MaTaiKhoan=a.MaTaiKhoan";
        SqlCommand cmd = new SqlCommand(strQuery, Helper.ConnectToDataBase());

        try
        {
            rptBasic.DataSource = cmd.ExecuteReader();
            rptBasic.DataBind();
            Helper.DisposeCommand(ref cmd);
        }
        catch (Exception ex)
        {
            Helper.DisposeCommand(ref cmd);
        }
    }

    private bool CheckForLogin()
    {
        string maTaiKhoan = GetIDUserLogin();
        if (maTaiKhoan.Length > 0)
            return true;
        return false;
    }

    private string GetIDUserLogin()
    {
        string userid = "";
        if (Request.Cookies["daugia_user"] != null)
            userid = Request.Cookies["daugia_user"]["userid"];
        return userid;
    }
}
